package com.lc.user.express.preferentialactivity;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.lc.user.express.BaseFragment;
import com.lc.user.express.R;
import com.lc.user.express.adapter.PreferentialAdapter;
import com.lc.user.express.httpserver.GetActivityList;
import com.lc.user.express.model.PreferentialModel;
import com.lc.user.express.ordermanagement.OrderManagementFragment;
import com.lc.user.express.view.pull.PullToRefreshBase;
import com.lc.user.express.view.pull.PullToRefreshListView;
import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;
import java.util.List;

import static com.lc.user.express.ordermanagement.OrderManagementFragment.onRefreshOrderData;


/**
 * Created by Administrator on 2016/2/26.优惠活动
 */
public class PreferentialActivityFragment extends BaseFragment {

    private ListView preferen_list;
    private List<PreferentialModel> list = new ArrayList<>();
    private PreferentialAdapter preferentialAdapter;
    private PullToRefreshListView pListView;
    @Override
    public View getContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        // TODO Auto-generated method stub
        View v = inflater.inflate(R.layout.fragment_preferential_activity, container, false);
        setTitle("优惠活动");
        preferen_list = (ListView) v.findViewById(R.id.preferen_list);
        preferentialAdapter = new PreferentialAdapter(activity, list);
        preferen_list.setAdapter(preferentialAdapter);
        initView(v);
        getData();
        preferen_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                bundle.putString("id",list.get(position).getId());
                bundle.putString("title",list.get(position).getTitle());
                show(ActivityWebActivity.class,bundle);
            }
        });




        return v;
    }


    private void initView(View v){
        pListView = (PullToRefreshListView) v.findViewById(R.id.lv);
        pListView.setPullLoadEnabled(false);
        pListView.setScrollLoadEnabled(true);
        pListView.setPullRefreshEnabled(true);
        pListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
               getData();
            }
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                getData();
            }
        });
        final ListView listView = pListView.getRefreshableView();
        listView.setDivider(null);
        listView.setDividerHeight(0);
        listView.setSelector(new ColorDrawable(Color.TRANSPARENT));
        preferentialAdapter = new PreferentialAdapter(getActivity(), list);
        listView.setAdapter(preferentialAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                bundle.putString("id",list.get(position).getId());
                bundle.putString("title",list.get(position).getTitle());
                show(ActivityWebActivity.class,bundle);
            }

        });



        onRefreshOrderData = new OrderManagementFragment.OnRefreshOrderData() {
            @Override
            public void onRefresh() {
            getData();
            }
        };
    }

    private void getData() {
        new GetActivityList(new AsyCallBack<GetActivityList.Info>() {
            @Override
            public void onSuccess(String toast, int type, GetActivityList.Info info) throws Exception {
                super.onSuccess(toast, type, info);
                list.clear();
                list.addAll(info.dataList);
                preferentialAdapter.updateListView(list);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);

                pListView.onPullDownRefreshComplete();
                pListView.onPullUpRefreshComplete();
            }
        }).execute(activity);//调接口
    }
}
